/* Copyright (C) 2012 Ben Lewis <benjf5@gmail.com>
 * twitter.h, a component of CLI-µblogger
 * 
 * CLI-µblogger is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <oauth.h>
#include <string>
#include <time.h>
#include <boost/lexical_cast.hpp>
#include <curl/curl.h>
// this next one isn't in the repository, and I know the code won't work without
// it.  I'm sorry.
#include "secrets.h"
// contains OAUTH_TOKEN, CONSUMER_KEY, TOKEN_SECRET and CONSUMER_SECRET.

/* implements access to the twitter APIs for post.h, since it makes sense to use
 * separate libraries for each individual service, and I'm not sure I want
 * anything to do with the twitcurl library. I dislike its license.
 */

char* update_twitter( std::string status ) {
  CURL *curl_handle = curl_easy_init();
  
  const std::string HTTP_METHOD = "POST";
  const std::string BASE_URL = "https://api.twitter.com/1/statuses/update.json";
  const std::string SIG_METHOD = "HMAC-SHA1";
  const std::string INC_ENTITIES = "true";
  const std::string OAUTH_VERSION = "1.0";
  const std::string OAUTH_NONCE = std::string( oauth::oauth_gen_nonce() );
  std::string encoded_status = std::string( oauth::oauth_url_escape( status.c_str() ) );
  // Gotta love dealing with C operators from C++.

  const time_t REQUEST_TIME = time();
  
  std::string PARAMETER_STRING = "include_entities=" + INC_ENTITIES + "&"
    + "oauth_consumer_key=" + CONSUMER_KEY + "&"
    + "oauth_nonce=" + OAUTH_NONCE + "&"
    + "oauth_signature_method=" + SIG_METHOD + "&"
    + "oauth_timestamp=" + boost::lexical_cast<string>(REQUEST_TIME) + "&"
    + "oauth_token=" + OAUTH_TOKEN + "&"
    + "oauth_version=" + OAUTH_VERSION + "&"
    + "status=" + encoded_status;

  std::string SIG_BASE_STRING = HTTP_METHOD + "&"
    + std::string( oauth::oauth_url_escape( BASE_URL.c_str() ) ) + "&"
    + std::string( oauth::oauth_url_escape( PARAMETER_STRING.c_str() ) );

  std::string SIGNING_KEY = CONSUMER_SECRET + "&" + TOKEN_SECRET;
  
  std::string MESSAGE_SIG = std::string( 
					oauth::oauth_sign_hmac_sha1( 
								    SIG_BASE_STRING.c_str(),
								    SIGNING_KEY.c_str()	)); 
  std::string auth_header_string = "OAuth ";
  auth_header_string += "oauth_consumer_key=" + '"' + CONSUMER_KEY + '"' + ',' + ' '
    + "oauth_nonce=" + '"' + OAUTH_NONCE + '"' + ',' + ' '
    + "oauth_signature=" + '"' + std::string( oauth::oauth_url_escape(MESSAGE_SIG.c_str())) + '"' + ',' + ' '
    + "oauth_signature_method." + '"' + SIG_METHOD + '"' + ',' + ' '
    + "oauth_timestamp=" + '"' + boost::lexical_cast<string>(REQUEST_TIME) + '"'
					+ ',' + ' '
    + "oauth_token=" + '"' + OAUTH_TOKEN + '"' + ',' + ' '
    + "oauth_version=" + '"' + OAUTH_VERSION + '"';

  curl_easy_setopt(curl_handle, CURLOPT_URL, BASE_URL.c_str() );
  struct curl_slist *curl_header = NULL;
  curl_header = curl_slist_append(curl_header, "Expect:");
  curl_header = curl_slist_append(curl_header, "Accept: */*");
  curl_header = curl_slist_append(curl_header, "Connection: close");
  curl_header = curl_slist_append(curl_header, "User-Agent: liboauth v.???");
  curl_header = curl_slist_append(curl_header, "Authorization: " +
				  auth_header_string.c_str());
  curl_header = curl_slist_append(curl_header, "Host: api.twitter.com");
  
  encoded_status = "status=" + encoded_status;
  char *status_ptr = encoded_status.c_str();
  
  curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, status_ptr );

  curl_easy_perform(curl_handle);
  curl_easy_cleanup(curl_handle);
}
